<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>代码生成管理</title>

    <!-- Bootstrap -->
    <link href="./bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">

    <style>
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
            position: relative;
        }

        .ctl-wrapper {
            width: 1000px;
            margin: 0 auto;
            position: relative;
            top: 0;
            bottom: 0;
            height: 100%;
        }

        .ctl-top {
            position: absolute;
            top: 0;
            width: 100%;
            text-align: center;
        }

        #html-log {
            border-radius: 4px;
            background-color: #0f0f0f;
            color: white;
            font-size: 13px;
            padding: 10px;
            overflow-y: auto;
            position: absolute;
            top: 80px;
            bottom: 20px;
            width: 100%;
        }
    </style>
</head>
<body>
<div class="container-fluid" style="position: absolute;top: 0;bottom: 0;width: 100%;">

    <div class="ctl-wrapper">
        <div class="ctl-top">
            <br>
            <button id="startBtn" type="button" class="btn btn-danger" onclick="start(this)"> 启动服务</button>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn btn-default" value="打开项目管理" onclick="openTab()"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn btn-default" value="系统配置" onclick="openSetting()"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" class="btn btn-info" value="浏览器打开【项目管理】" onclick="openInBrowser()"/>
            <br/>
            <div id="showMsg"></div>
        </div>

        <div id="html-log">
            Hello Auto Code！
        </div>
    </div>


    <!-- 提示框 -->
    <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
        <div class="modal-dialog modal-sm" role="document">
            <div class="modal-content">
                未配置，请先配置服务。
            </div>
        </div>
    </div>

</div>
<script type="text/javascript">

    var DispatcherHandle = 'test';
    var isRunning = false;

    function cliStart() {
        if (!isRunning) {
            start();
        }
    }

    function start() {

        isRunning = true;

        var obj = {type: "start", params: {name: "08888"}};
        var res = DispatcherHandle.dispatcher(JSON.stringify(obj));
        var retObj = JSON.parse(res);
        var code = retObj.code;

        // 检测服务启动情况: 2001 启动中；3001 启动失败；2002 运行中；3002 未配置文件
        if (code == 2001) {
            $('#startBtn').html('启动中...');
            $('#showMsg').html('');

            // 检查并更新状态
            setTimeout(function () {
                start();
            }, 1000);
        } else if (code == 2002) {
            $('#startBtn').html('运行中...');
            $('#showMsg').html('');
            $('#startBtn').removeClass('btn-danger');
            $('#startBtn').addClass('btn-success');
        } else if (code == 3001) {
            $('#showMsg').html('<p class="bg-warning">启动失败！请查看日志。</p>');
            // open 日志文件
            isRunning = false;
            $('#startBtn').html('启动服务');
        } else if (code == 3002) {
            $('#showMsg').html('<p class="bg-warning">系统配置未设置，请先设置。</p>');
            isRunning = false;
            $('#startBtn').html('启动服务');
            setTimeout(function () {
                openSetting();
            }, 1200);
        }

        fetchLog();
    }

    function fetchLog() {
        var obj = {type: "fetchLog", params: {name: "08888"}};
        var res = DispatcherHandle.dispatcher(JSON.stringify(obj));

        if (res) {
            var obj = JSON.parse(res);
            if (obj['text']) {
                $('#html-log').append("<br>" + obj['text']);

                var element = document.getElementById("html-log");
                element.scrollTop = element.scrollHeight;
                setTimeout(fetchLog, 200);
            } else {
                setTimeout(fetchLog, 3000);
            }
        } else {
            setTimeout(fetchLog, 3000);
        }

    }

    // 在浏览器中打开服务管理页面
    function openInBrowser() {
        var obj = {type: "openInBrowser", params: {}};
        DispatcherHandle.dispatcher(JSON.stringify(obj));
    }

    function openTab() {
        var obj = {type: "openTab", params: {url: "http://localhost:9090/code_main/index.html"}};
        DispatcherHandle.dispatcher(JSON.stringify(obj));
    }

    function openSetting() {
        var obj = {type: "openSetting", params: {}};
        DispatcherHandle.dispatcher(JSON.stringify(obj));
    }

    // 获取服务运行状态
    setTimeout(function () {
        start();
    }, 1000);

</script>

<script src="./jquery/jquery.min.js"></script>
<script src="./bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</body>
</html>